package com.site.springboot.core.controller.admin;

import com.site.springboot.core.entity.NewsIndex;
import com.site.springboot.core.repository.NewsIndexRepository;
import com.site.springboot.core.service.impl.NewsIndexService;
import com.site.springboot.core.util.Result;
import com.site.springboot.core.util.ResultGenerator;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Project:springboot-project-news-publish-system
 * ClassName:ElasticSearchController
 * Package:com.site.springboot.core.controller.admin
 * User:CWQ
 * Description:
 *
 * @Author 合肥-陈万琼
 * @Create 2024/5/29 0:12
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class ElasticSearchController {
    @Resource
    private NewsIndexService newsIndexService;
    @Resource
    private NewsIndexRepository newsIndexRepository;

    @RequestMapping("/init")
    public Result elasticSearch() {
        newsIndexService.initIndex();
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/search1")
    public Result search() {
        Optional<NewsIndex> contentsOpt = newsIndexRepository.findById(1L);
        if (contentsOpt.isPresent()) {
            NewsIndex newsId1 = contentsOpt.get();
            System.out.println("id=1:" + newsId1);
        }

        // 批量ID查询
        for (NewsIndex newsIndex : newsIndexRepository
                .findAllById(Arrays.asList(10L, 12L))) {
            System.out.println("id=" + newsIndex.getNewsId() + ":" + newsIndex);
        }
        return ResultGenerator.genSuccessResult();
    }

}
